<?php

    // <getposts>
    function CreateGetPostsXMLInfomat($speciality_codes, $datebegin, $dateend)
    {
        $doc = new DOMDocument();
        $doc->formatOutput = true;
        $root = $doc->createElement("getpostlist");
        $root->setAttribute('datebegin', $datebegin);
        $root->setAttribute('dateend', $dateend);
        foreach($speciality_codes as $code)
        {
            $speciality = $doc->createElement("speciality");   
            $speciality->setAttribute('code', $code);
            $root->appendChild($speciality);
        }
        $doc->appendChild($root);        
        return $doc->SaveXML();
    }
    
    // <DoRegistry>
    function CreateDoRegistryXML($cardid, $ticketid)
    {
        $doc = new DOMDocument();
        $doc->formatOutput = true;
        $auth = $doc->createElement("doregistry");
        $auth->setAttribute('cardid', $cardid);
        $auth->setAttribute('ticketid', $ticketid);
        $doc->appendChild($auth);
        return $doc->SaveXML();
    }

    // <login>
    function CreateLoginXML($slogin, $spassword)
    {
        $doc = new DOMDocument();
        $doc->formatOutput = true;
        $auth = $doc->createElement('authentification');
        //$text = $doc->createTextNode('Description');
        $auth->setAttribute('login', $slogin);
        $auth->setAttribute('password', $spassword);
        $doc->appendChild($auth);
        return $doc->SaveXML();
    }

    /**
    *  getdictionaries
    */
    /*
    function CreateGetDictionariesXML($datebegin, $dateend)
    {
        $doc = new DOMDocument();
        $doc->formatOutput = true;
        $root = $doc->createElement('getdictionaries');
        $root->setAttribute('datebegin', $datebegin);
        $root->setAttribute('dateend', $dateend);
        $doc->appendChild($root);
        return $doc->SaveXML();
    }
    */

    // <remoterecord>
    function CreateRemoterecordXML($date, $patient_info, $tickets)
    {
        $doc = new DOMDocument();
        $doc->formatOutput = true;
        $root = $doc->createElement("remotedirection");

        $root->setAttribute('target_date', $patient_info['target_date']);
        $root->setAttribute('direction_perpose', $patient_info['direction_perpose']);
        unset($patient_info['target_date'], $patient_info['direction_perpose']);

        $patient = $doc->createElement("patient");
        $records = $doc->createElement("entries");
        foreach($patient_info as $key => $value)
        {
            if(is_array($value))
            {
                foreach($value as $key2 => $value2)
                {
                    if(!is_array($value2)){
                    $patient->setAttribute($key2, cnv_to($value2));}
                }
            }
            else
            {
                $patient->setAttribute($key, cnv_to($value));
            }
        }
        foreach($tickets as $ticket_code)
        {
            $record = $doc->createElement('entry');   
            $record->setAttribute('code', $ticket_code);
            $records->appendChild($record);
        }
        $root->appendChild($patient);
        $root->appendChild($records);
        $doc->appendChild($root);     
        return $doc->SaveXML();
    }

    function CreateGetRemoteDirectionsXML($remotedirection = null, $method = 'part', $month, $year)
    {
        $doc = new DOMDocument();
        $doc->formatOutput = true;
        $root = $doc->createElement("getremotedirections");
        $root->setAttribute('method', $method);
        $root->setAttribute('month', $month);
        $root->setAttribute('year', $year);
        if($remotedirection != null)
        {
            $speciality = $doc->createElement("remotedirection");   
            $speciality->setAttribute('guid', $remotedirection);
            $root->appendChild($speciality);
        }
        $doc->appendChild($root);        
        return $doc->SaveXML();  
    }

    function CreateDeleteRemoteDirectionXML($remotedirection)
    {
        $doc = new DOMDocument();
        $doc->formatOutput = true;
        $root = $doc->createElement("deleteremotedirection");
        $root->setAttribute('guid', $remotedirection);
        $doc->appendChild($root);        
        return $doc->SaveXML();  
    }

?>